﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class OrderSelectionBLL : IBaseInfo
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IOrderSelectionDA OrderSelectionDA = DataAccess.CreateOrderSelectionDA();
        private PubParasInfo pubParasInfo;

        public OrderSelectionBLL()
        {
            this.pubParasInfo = null;
        }

        public OrderSelectionBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public OrderSelectionBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string Delete(List<string> OrderSelectionIDS)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                List<OrderSelectionInfo> orderSelectionInfos = new List<OrderSelectionInfo>();
                OrderSelectionInfo orderSelectionInfo = null;
                foreach (string id in OrderSelectionIDS)
                {
                    orderSelectionInfo = OrderSelectionDA.GetOrderSelectionByID(dataBase, tran, id, this.dataBaseID[0]);
                    orderSelectionInfos.Add(orderSelectionInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (OrderSelectionDA.DeleteOrderSelection(dataBase, tran, OrderSelectionIDS, this.dataBaseID) > 0)
                {
                    for (int i = 0; i < OrderSelectionIDS.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<OrderSelectionInfo>(dataBase, tran, orderSelectionInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "ORDER_SELECTION", this.dataBaseID[0]);
                        temp = temp + OrderSelectionIDS[i] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllInfo()
        {
            return OrderSelectionDA.GetAllOrderSelection(this.dataBaseID[0]);
        }

        public object GetInfoByID(string OrderSelectionID)
        {
            return OrderSelectionDA.GetOrderSelectionByID(OrderSelectionID, this.dataBaseID[0]);
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            OrderSelectionQueryEntity orderSelectionQuery = obj as OrderSelectionQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(orderSelectionQuery.OrderSelectionID))
            {
                sqlWhere.Add(" ORDER_SELECTION_ID " + orderSelectionQuery.OrderSelectionID);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.IsDefault))
            {
                sqlWhere.Add(" IS_DEFAULT " + orderSelectionQuery.IsDefault);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.OrderStatusStart))
            {
                sqlWhere.Add(" ORDER_STATUS_START " + orderSelectionQuery.OrderStatusStart);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.OrderStatusEnd))
            {
                sqlWhere.Add(" ORDER_STATUS_END " + orderSelectionQuery.OrderStatusEnd);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.OrderDateStart))
            {
                sqlWhere.Add(" ORDER_DATE_START " + orderSelectionQuery.OrderDateStart);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.OrderDateEnd))
            {
                sqlWhere.Add(" ORDER_DATE_END " + orderSelectionQuery.OrderDateEnd);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.DeliveryDateStart))
            {
                sqlWhere.Add(" DELIVERY_DATE_START " + orderSelectionQuery.DeliveryDateStart);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.DeliveryDateEnd))
            {
                sqlWhere.Add(" DELIVERY_DATE_END " + orderSelectionQuery.DeliveryDateEnd);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.OrderTypeStart))
            {
                sqlWhere.Add(" ORDER_TYPE_START " + orderSelectionQuery.OrderTypeStart);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.OrderTypeEnd))
            {
                sqlWhere.Add(" ORDER_TYPE_END " + orderSelectionQuery.OrderTypeEnd);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.OrderGroupStart))
            {
                sqlWhere.Add(" ORDER_GROUP_START " + orderSelectionQuery.OrderGroupStart);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.OrderGroupEnd))
            {
                sqlWhere.Add(" ORDER_GROUP_END " + orderSelectionQuery.OrderGroupEnd);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.OrderPriorityStart))
            {
                sqlWhere.Add(" ORDER_PRIORITY_START " + orderSelectionQuery.OrderPriorityStart);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.OrderPriorityEnd))
            {
                sqlWhere.Add(" ORDER_PRIORITY_END " + orderSelectionQuery.OrderPriorityEnd);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.OwnerIdStart))
            {
                sqlWhere.Add(" OWNER_ID_START " + orderSelectionQuery.OwnerIdStart);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.OwnerIdEnd))
            {
                sqlWhere.Add(" OWNER_ID_END " + orderSelectionQuery.OwnerIdEnd);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.OrderIdStart))
            {
                sqlWhere.Add(" ORDER_ID_START " + orderSelectionQuery.OrderIdStart);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.OrderIdEnd))
            {
                sqlWhere.Add(" ORDER_ID_END " + orderSelectionQuery.OrderIdEnd);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.OrderIdStart))
            {
                sqlWhere.Add(" EXTERN_ORDER_ID_START " + orderSelectionQuery.ExternOrderIdStart);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.OrderIdEnd))
            {
                sqlWhere.Add(" EXTERN_ORDER_ID_END " + orderSelectionQuery.ExternOrderIdEnd);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.ConsigneeIdStart))
            {
                sqlWhere.Add(" CONSIGNEE_ID_START " + orderSelectionQuery.ConsigneeIdStart);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.ConsigneeIdEnd))
            {
                sqlWhere.Add(" CONSIGNEE_ID_END " + orderSelectionQuery.ConsigneeIdEnd);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.CarrierIdStart))
            {
                sqlWhere.Add(" CARRIER_ID_START " + orderSelectionQuery.CarrierIdStart);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.CarrierIdEnd))
            {
                sqlWhere.Add(" CARRIER_ID_END " + orderSelectionQuery.CarrierIdEnd);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.RouteStart))
            {
                sqlWhere.Add(" ROUTE_START " + orderSelectionQuery.RouteStart);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.RouteEnd))
            {
                sqlWhere.Add(" ROUTE_END " + orderSelectionQuery.RouteEnd);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.DoorStart))
            {
                sqlWhere.Add(" DOOR_START " + orderSelectionQuery.DoorStart);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.DoorEnd))
            {
                sqlWhere.Add(" DOOR_END " + orderSelectionQuery.DoorEnd);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.DoorStart))
            {
                sqlWhere.Add(" DOOR_START " + orderSelectionQuery.DoorStart);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.DoorEnd))
            {
                sqlWhere.Add(" DOOR_END " + orderSelectionQuery.DoorEnd);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.StopStart))
            {
                sqlWhere.Add(" STOP_START " + orderSelectionQuery.StopStart);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.StopEnd))
            {
                sqlWhere.Add(" STOP_END " + orderSelectionQuery.StopEnd);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.StopStart))
            {
                sqlWhere.Add(" STOP_START " + orderSelectionQuery.StopStart);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.StopEnd))
            {
                sqlWhere.Add(" STOP_END " + orderSelectionQuery.StopEnd);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.LoadIdStart))
            {
                sqlWhere.Add(" LOAD_ID_START " + orderSelectionQuery.LoadIdStart);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.LoadIdEnd))
            {
                sqlWhere.Add(" LOAD_ID_END " + orderSelectionQuery.LoadIdEnd);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.CompanyStart))
            {
                sqlWhere.Add(" COMPANY_START " + orderSelectionQuery.CompanyStart);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.CompanyEnd))
            {
                sqlWhere.Add(" COMPANY_END " + orderSelectionQuery.CompanyEnd);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.StateStart))
            {
                sqlWhere.Add(" STATE_START " + orderSelectionQuery.StateStart);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.StateEnd))
            {
                sqlWhere.Add(" STATE_END " + orderSelectionQuery.StateEnd);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.ZipStart))
            {
                sqlWhere.Add(" ZIP_START " + orderSelectionQuery.ZipStart);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.ZipEnd))
            {
                sqlWhere.Add(" ZIP_END " + orderSelectionQuery.ZipEnd);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.CityBegin))
            {
                sqlWhere.Add(" CITY_BEGIN " + orderSelectionQuery.CityBegin);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.CityEnd))
            {
                sqlWhere.Add(" CITY_END " + orderSelectionQuery.CityEnd);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.CartonGroup))
            {
                sqlWhere.Add(" CARTON_GROUP " + orderSelectionQuery.CartonGroup);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.EnablePacking))
            {
                sqlWhere.Add(" ENABLE_PACKING " + orderSelectionQuery.EnablePacking);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.MaxOrders))
            {
                sqlWhere.Add(" MAX_ORDERS " + orderSelectionQuery.MaxOrders);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.BatchPickMaxCube))
            {
                sqlWhere.Add(" BATCH_PICK_MAX_CUBE " + orderSelectionQuery.BatchPickMaxCube);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.BatchPickMaxCount))
            {
                sqlWhere.Add(" BATCH_PICK_MAX_COUNT " + orderSelectionQuery.BatchPickMaxCount);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.OrderHandingTypeStart))
            {
                sqlWhere.Add(" ORDER_HANDING_TYPE_START " + orderSelectionQuery.OrderHandingTypeStart);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.OrderHandingTypeEnd))
            {
                sqlWhere.Add(" ORDER_HANDING_TYPE_END " + orderSelectionQuery.OrderHandingTypeEnd);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.SkuClassIdStart))
            {
                sqlWhere.Add(" SKU_CLASS_ID_START " + orderSelectionQuery.SkuClassIdStart);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.SkuClassIdEnd))
            {
                sqlWhere.Add(" SKU_CLASS_ID_END " + orderSelectionQuery.SkuClassIdEnd);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.SkuGroupStart))
            {
                sqlWhere.Add(" SKU_GROUP_START " + orderSelectionQuery.SkuGroupStart);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.SkuGroupEnd))
            {
                sqlWhere.Add(" SKU_GROUP_END " + orderSelectionQuery.SkuGroupEnd);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.SkuGroup2Start))
            {
                sqlWhere.Add(" SKU_GROUP2_START " + orderSelectionQuery.SkuGroup2Start);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.SkuGroup2End))
            {
                sqlWhere.Add(" SKU_GROUP2_END " + orderSelectionQuery.SkuGroup2End);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.SkuIdStart))
            {
                sqlWhere.Add(" SKU_ID_START " + orderSelectionQuery.SkuIdStart);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.SkuIdEnd))
            {
                sqlWhere.Add(" SKU_ID_END " + orderSelectionQuery.SkuIdEnd);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.AbcStart))
            {
                sqlWhere.Add(" ABC_START " + orderSelectionQuery.AbcStart);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.AbcEnd))
            {
                sqlWhere.Add(" ABC_END " + orderSelectionQuery.AbcEnd);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.LotStart))
            {
                sqlWhere.Add(" LOT_START " + orderSelectionQuery.LotStart);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.LotEnd))
            {
                sqlWhere.Add(" LOT_END " + orderSelectionQuery.LotEnd);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.Udf1Start))
            {
                sqlWhere.Add(" UDF1_START " + orderSelectionQuery.Udf1Start);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.Udf1End))
            {
                sqlWhere.Add(" UDF1_END " + orderSelectionQuery.Udf1End);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.Udf2Start))
            {
                sqlWhere.Add(" UDF2_START " + orderSelectionQuery.Udf2Start);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.Udf2End))
            {
                sqlWhere.Add(" UDF2_END " + orderSelectionQuery.Udf2End);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.Udf3Start))
            {
                sqlWhere.Add(" UDF3_START " + orderSelectionQuery.Udf3Start);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.Udf3End))
            {
                sqlWhere.Add(" UDF3_END " + orderSelectionQuery.Udf3End);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.Udf4Start))
            {
                sqlWhere.Add(" UDF4_START " + orderSelectionQuery.Udf4Start);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.Udf4End))
            {
                sqlWhere.Add(" UDF4_END " + orderSelectionQuery.Udf4End);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.Udf5Start))
            {
                sqlWhere.Add(" UDF5_START " + orderSelectionQuery.Udf5Start);
            }
            if (!string.IsNullOrEmpty(orderSelectionQuery.Udf5End))
            {
                sqlWhere.Add(" UDF5_END " + orderSelectionQuery.Udf5End);
            }
            return OrderSelectionDA.GetOrderSelectionByQueryList(sqlWhere, orderSelectionQuery, this.dataBaseID[0]);
        }

        public string Insert(object obj)
        {
            string OrderSelectionID = string.Empty;
            try
            {
                OrderSelectionInfo orderSelectionInfo = obj as OrderSelectionInfo;
                if (string.IsNullOrEmpty(orderSelectionInfo.OrderSelectionID))
                {
                    orderSelectionInfo.OrderSelectionID = commonMethod.GenNextNumStr("ORDER_SELECTION", this.dataBaseID[0]);
                }
                if (OrderSelectionDA.CheckOrderSelectionIDUnique(orderSelectionInfo.OrderSelectionID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F7200_001", this.pubParasInfo.UserLanguage, new List<string> { orderSelectionInfo.OrderSelectionID });
                }
                orderSelectionInfo.UpdatedBy = orderSelectionInfo.CreatedBy;
                orderSelectionInfo.CreatedDate = DateTime.Now;
                orderSelectionInfo.UpdatedDate = DateTime.Now;
                if (OrderSelectionDA.InsertOrderSelection(orderSelectionInfo, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<OrderSelectionInfo>(null, orderSelectionInfo, AuditAction.Insert, this.pubParasInfo.UserID, "ORDER_SELECTION", this.dataBaseID[0]);
                    OrderSelectionID = orderSelectionInfo.OrderSelectionID;
                }
                return OrderSelectionID;
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return OrderSelectionID;
        }

        public string Update(object obj)
        {
            string OrderSelectionID = string.Empty;
            try
            {
                OrderSelectionInfo orderSelectionInfo = obj as OrderSelectionInfo;
                orderSelectionInfo.UpdatedDate = DateTime.Now;
                OrderSelectionInfo oldOrderSelectionInfo = OrderSelectionDA.GetOrderSelectionByID(orderSelectionInfo.OrderSelectionID, this.dataBaseID[0]);
                if (OrderSelectionDA.UpdateOrderSelection(orderSelectionInfo, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<OrderSelectionInfo>(oldOrderSelectionInfo, orderSelectionInfo, AuditAction.Update, this.pubParasInfo.UserID, "ORDER_SELECTION", this.dataBaseID[0]);
                    OrderSelectionID = orderSelectionInfo.OrderSelectionID;
                }
                return OrderSelectionID;
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return OrderSelectionID;
        }
    }
}

